package com.tadpole.music.presenter.me;

import com.tadpole.music.bean.me.WeChatPayBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.WeChatPayIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 微信支付
 * Created By 孙岩
 * on 2020/9/23
 */
public class WeChatPayPresenter extends BasePresenter<WeChatPayIView> {
    private String url;
    /**
     *
     * @param flag //flag = 1是题库 flag = 2是套题
     * @param goods_id
     * @param suit_id 套题id
     */
    @SuppressWarnings("unchecked")
    public void weChatPay(String flag,String goods_id,String suit_id) {
        if (isViewAttached()) {
            if("1".equals(flag)){
                url = UrlConfig.codePay + goods_id;
            }else if("2".equals(flag)){
                url = UrlConfig.purchaseQuestion+suit_id;
            }
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .paramKey("pay_type")
                    .paramValue("3")
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    WeChatPayBean.DataBean bean = new WeChatPayBean.DataBean(
                                            JsonParseUtil.getStr(object, "appid"),
                                            JsonParseUtil.getStr(object, "noncestr"),
                                            JsonParseUtil.getStr(object, "package"),
                                            JsonParseUtil.getStr(object, "partnerid"),
                                            JsonParseUtil.getStr(object, "prepayid"),
                                            JsonParseUtil.getStr(object, "sign"),
                                            JsonParseUtil.getStr(object, "timestamp")
                                    );
                                    getView().showWeChatResult(bean);

                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
